package com.ass.basicmanage.web;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.ass.base.model.PageGrid;
import com.ass.base.model.PageQueryModel;
import com.ass.base.web.BaseController;
import com.ass.common.constant.CommonConstant;
import com.ass.common.generated.dao.TPermissionMapper;
import com.ass.common.generated.model.TPermission;
import com.ass.common.service.CommonService;
import com.ass.common.utils.JsonUtil;
import com.ass.common.utils.StringUtil;

/**
 * 通用查询方法
 * 
 * @author wangt
 * @time 20140225
 * 
 */
@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController {

	@Resource
	private CommonService commonService;
	
	@Resource
	private TPermissionMapper tPermissionMapper;
	
	
	@RequestMapping(value = "/getMenusByUserid")
	public void testJson(HttpServletRequest request, HttpServletResponse response) {
		
		Long id = this.getUser().getId();
		String sql =  " SELECT distinct p.id, p.pid, p.name, p.permission,p.lev, p.value url, p.position FROM t_permission p "
						+" LEFT JOIN t_role_permission rp ON rp.t_permission_id = p.id "
						+" LEFT JOIN t_user_role ur ON ur.t_role_id = rp.t_role_id "
						+" LEFT JOIN t_user u ON u.id = ur.t_user_id "
						+" WHERE p.lev != 0 and  u.id = "+ StringUtil.getString(id)+" "
						+ " order by p.lev asc, p.position asc ";
		List<Map<String, Object>> lst = commonService.selectBySql(sql);
		this.printStr(response, JsonUtil.writeValue(lst));
	}

	
	//跳转到编辑菜单页面
	@RequestMapping (value="/toUpdateMenu")
	public ModelAndView toUpdateMenuInfo (HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("ass/basicmanage/menu/menuUpdate");
		String id = request.getParameter("id");
		if (StringUtil.isNotEmpty(id)) {
			mav.addObject("entity", tPermissionMapper.selectByPrimaryKey(Long.parseLong(id)));
		}else{
			TPermission tp = new TPermission();
			tp.setId(0l);
			mav.addObject("entity", tp);
		}
		mav.addObject("statement", request.getParameter("statement"));
		mav.addObject("tourl", request.getParameter("tourl"));
		return mav;
	}
	
	
	//新增,编辑菜单
	@RequestMapping (value="/saveMenu")
	public ModelAndView saveMenu (HttpServletRequest request,TPermission tp) {
		if(0l == (tp.getId())){
			//insert
			if(tp.getLev() == 1){
				//一级菜单
				tp.setPid(0);
				tp.setValue("");
			}
			tPermissionMapper.insertSelective(tp);
		}else{
			//update
			if(tp.getLev() == 1){
				//一级菜单
				tp.setPid(0);
				tp.setValue("");
			}
			tPermissionMapper.updateByPrimaryKeySelective(tp);
			
		}
		String tourl = request.getParameter("tourl");
		String statement = request.getParameter("statement");
		return commonService.search(tourl, statement);
	}
	
	
	
	@RequestMapping (value="/delete")
	public void deleteMenu (HttpServletRequest request,HttpServletResponse response) {
		String id = request.getParameter("id");
		tPermissionMapper.deleteByPrimaryKey(Long.parseLong(id));
		this.printStr(response, JSON.toJSONString(this.getMap()));
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public static void main(String[] args) {
		System.out.println(StringUtil.getString(null));
		System.out.println(StringUtil.getString("  "));
		System.out.println(StringUtil.getString(""));
	}
	
	

}
